<?php
/**
 *
 * @author griever
 * @version 
 */

require_once 'Base.php';

/**
 * ValidateCommitRequest Action Helper 
 * 
 * @uses actionHelper Custom_Helper
 */
class Custom_Helper_ValidateCommitRequest extends Dummy_Helper_Base {
	
	private static $requiredKeys = array ("prop_key", "prop_val", "parent_key" );
	
	public function validateCommitRequest($json) {
		//dont decode null and empty value
		if ($json === null || $json === '') {
			return false;
		} else {
			try	{
				$data = Zend_Json::decode ( $json, Zend_Json::TYPE_ARRAY );
			} catch(Exception $ex)	{
				return false;
			}
			
			//dont decode non-array
			if ($data === null || ! is_array ( $data )) {
				return false;
			} else {
				if (! is_array ( $data )) {
					return false;
				} else {
					foreach ( $data as $key => $value ) {
						if (! is_array ( $value )) {
							return false;
						} else {
							//dont decode array missing keys
							foreach ( self::$requiredKeys as $reqKey ) {
								if (! array_key_exists ( $reqKey, $value )) {
									return false;
								}
							}
						}
					}
				}
			}
		}
		
		return $data;
	}
	
	/**
	 * Strategy pattern: call helper as broker method
	 */
	public function direct($json) {
		// TODO Auto-generated 'direct' method
		return $this->validateCommitRequest ( $json );
	}
}
